<?php
/**
 * Created by PhpStorm.
 * User: zsf
 * Date: 2018/5/14
 * Time: 9:39
 */

namespace App\Services\Authorize;

use App\Services\ResponseCode;
use App\Models\{DeviceLine\PubAccount, User\User, Enterprise\Enterprise, Enterprise\SuperEnterprise};
use App\Services\Setting;
use App\Services\Token as TokenService;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class RecodingDownloadSignature extends BaseSignature implements AuthorizeInterface
{

    const SIGNATURE_HEADER_KEYS = ['username','outline-number', 'timestamp', 'source', 'auth-type', 'nonce'];
    /**
     * @var $superEnterprise SuperEnterprise
     * @var $user User
     */
    protected $superEnterprise, $user, $username, $outline_number;

    /**
     * @return SuperEnterprise|null
     * @throws \App\Exceptions\ApiException
     * 获取当前用户
     */
    public function getUser(): ?User
    {

        if($user = $this->getLogin()){
            return $user;
        }

        $methods = get_class_methods($this);
        foreach ($methods as $method) {
            if (starts_with($method, '_validate')) {
                $this->$method();
            }
        }
        $this->verifyCaptcha();
        $pubaccount = PubAccount::select(["seid"])->where('outline_number',$this->outline_number)->first();
        if(empty($pubaccount)){
            api_exception(ResponseCode::TRANS_OUTLINE_NUMBER_NOT_EXISTS);
        }

        $this->user = User::where('username', $this->username)->where('seid', $pubaccount->seid)->first();
        if (!$this->user) {
            api_exception(ResponseCode::LOGIN_FAILED, '账号或密码错误');
        }
        if (!$this->user->is_email_verified) {
            api_exception(ResponseCode::USER_EMAIL_NOT_VERIFIED);
        }


        if ($this->validateSignature()) {
            return $this->user;
        }
        return null;

    }


    protected function getLogin(){

        $token = request()->header('token');
        if (empty($token)) {
           return false;
        }
        $tokenService = (new TokenService($token));
        $userData = $tokenService->getData();
        if (!$userData) {
            api_exception(ResponseCode::UNAUTHORIZE);
        }
        if ($tokenService->getData('source') != request()->header('source')) {
            api_exception(ResponseCode::TOKEN_IS_NOT_PREG_SOURCE);
        }

        $this->user = User::where('seid',$userData["seid"] )
            ->where('id', $userData["user_id"])
            ->first();

        if(!$this->user){
            api_exception(ResponseCode::SIGNATURE_ERROR);
        }

        return $this->user;



    }


    /**
     * @throws \App\Exceptions\ApiException
     * 校验用户名是否符合格式
     */
    protected function _validateUserName(): void
    {
        if (!request()->header('username')) {
            api_exception(ResponseCode::USERNAME_ERROR);
        }

        $this->username = request()->header('username');
    }

    /**
     * @throws \App\Exceptions\ApiException
     * 校验用户名是否符合格式
     */
    protected function _validateOutLineNumber(): void
    {
        if (!request()->header('outline-number')) {
            api_exception(ResponseCode::OUTLINE_NUMBER_IS_MUST);
        }

        $this->outline_number = request()->header('outline-number');

    }

    protected function getSignatureHeadKeys(): array
    {
        return self::SIGNATURE_HEADER_KEYS;
    }

    /**
     * 获取签名密钥
     * @return mixed
     */
    protected function getSignatureKey(): ?string
    {
        return $this->user['password'];
    }


}